//
//  DYZXView.m
//  HBuilder
//
//  Created by 黄德玉 on 2019/11/2.
//  Copyright © 2019 DCloud. All rights reserved.
//

#import "DYZXView.h"
#import "DYGlobal.h"
#import "Masonry.h"
#import "DYInputMoneyView.h"
#import "EBHNetWorkingManager.h"
#import "UIView+MBProgressHUD.h"

@implementation DYZXView

- (instancetype)init{
    if (self = [super init]) {
        self.topBgView = [UIView new];
        self.topBgView.backgroundColor = HexRGB(0XEDE1A6);
        [self addSubview:self.topBgView];
        [self.topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.height.equalTo(@22);
        }];
        self.topLbl = [UILabel new];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"本页面针对患者单独设置咨询费" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        self.topLbl.attributedText = string;
        [self addSubview:self.topLbl];
        [self.topLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.centerY.equalTo(self.topBgView);
        }];
        
        self.moneyLbl = [UILabel new];
        NSMutableAttributedString *Lblstring = [[NSMutableAttributedString alloc] initWithString:@"￥35/次" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],NSForegroundColorAttributeName: [UIColor colorWithRed:173/255.0 green:133/255.0 blue:252/255.0 alpha:1.0]}];
        self.moneyLbl.attributedText = Lblstring;
        [self addSubview:self.moneyLbl];
        [self.moneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.top.equalTo(self).offset(54);
        }];
        
        self.changeMoneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.changeMoneyBtn setTitle:@"单独修改" forState:UIControlStateNormal];
        [self.changeMoneyBtn setTitleColor:HexRGB(0x333333) forState:UIControlStateNormal];
        [self.changeMoneyBtn addTarget:self action:@selector(chageCliced) forControlEvents:UIControlEventTouchUpInside];
        self.changeMoneyBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [self addSubview:self.changeMoneyBtn];
        [self.changeMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-16);
            make.centerY.equalTo(self.moneyLbl);
        }];
        
        self.spliteView = [UIView new];
        self.spliteView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
        [self addSubview:self.spliteView];
        [self.spliteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@10);
            make.top.equalTo(self).offset(94);
        }];
        
        self.tipsTitle = [UILabel new];
        NSMutableAttributedString *tips = [[NSMutableAttributedString alloc] initWithString:@"温馨提示：" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];
        self.tipsTitle.attributedText = tips;
        [self addSubview:self.tipsTitle];
        [self.tipsTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.top.equalTo(self.spliteView.mas_bottom).offset(20);
        }];
        
        self.tips = [UILabel new];
        NSMutableAttributedString *tipsAttr = [[NSMutableAttributedString alloc] initWithString:@"1、修改咨询付费请参考患者付费能力；\n\n2、修改成功后，患者将从下一次咨询进行生效；\n\n3、咨询推广期暂不收取平台服务费；" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];
        self.tips.attributedText = tipsAttr;
        [self addSubview:self.tips];
        [self.tips mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.top.equalTo(self.tipsTitle.mas_bottom).offset(20);
        }];
        self.tips.numberOfLines = 0;
        
    }
    return self;
}

- (void)chageCliced{
    __weak typeof(self)weakSelf = self;
    [DYInputMoneyView showWithCallBack:^(id  _Nonnull value) {
        [weakSelf updateMoneyWithMoney:value];
    }];
}


- (void)updateMoneyWithMoney:(id)value{
    __weak typeof(self)weakSelf = self;
    if (!value) {
        value = @0;
    }
    [EBHNetWorkingManager queryWithPath:@"/sapi/hcp/servicePrice" pars:@{@"price":value,@"consumer":self.consumer} method:@"PUT" callback:^(NSError * _Nonnull error, id  _Nonnull response) {
        if (error) {
            [[UIApplication sharedApplication].keyWindow showHUDMessage:@"修改失败"];
            return;
        }
        [[UIApplication sharedApplication].keyWindow showHUDMessage:@"修改成功"];
        NSString * textValue = [NSString stringWithFormat:@"￥%ld/次", [value integerValue]];
        NSMutableAttributedString *Lblstring = [[NSMutableAttributedString alloc] initWithString:textValue attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17],NSForegroundColorAttributeName: [UIColor colorWithRed:173/255.0 green:133/255.0 blue:252/255.0 alpha:1.0]}];
        weakSelf.moneyLbl.attributedText = Lblstring;
    }];
}

@end
